package cn.ylz.ylmall.content.controller;

import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.common.web.JsonResult;
import cn.ylz.ylmall.content.pojo.param.LikeAddNewParam;
import cn.ylz.ylmall.content.pojo.vo.CountVO;
import cn.ylz.ylmall.content.service.ILikeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@Slf4j
@RequestMapping("/v1/like")
@Validated
@Api(tags = "2.3. 点赞模块")
public class LikeController {
    @Autowired
    private ILikeService iLikeService;

    @PostMapping("/addNew")
    @ApiOperation("点赞操作")
    @ApiOperationSupport(order = 301)
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                             @Valid LikeAddNewParam likeAddNewParam){
        log.debug("开始处理点赞操作 参数{}",likeAddNewParam);
        JsonResult IsLike = iLikeService.addNew(currentPrincipal,likeAddNewParam);
        return JsonResult.ok(IsLike);
    }

    @GetMapping("")
    @ApiOperation("统计点赞数量")
    public JsonResult count(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                            @Valid @RequestParam Long contentId){
        log.debug("开始处理统计点赞数量 参数{}",contentId);
        CountVO count = iLikeService.count(contentId);
        return JsonResult.ok(count);
    }

    @PostMapping("/IsLike")
    @ApiOperation("查询是否已经点赞")
    public Boolean IsLike(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                         @Valid @RequestParam Long contentId){
        log.debug("开始处理查询是否已经点赞 参数{}",contentId);
        return iLikeService.selectIsLike(currentPrincipal.getId(),contentId);
    }

}
